package com.atguigu.ggkt.vod.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.ggkt.exception.CustomException;
import com.atguigu.ggkt.model.vod.Subject;
import com.atguigu.ggkt.vo.vod.SubjectEeVo;
import com.atguigu.ggkt.vod.listener.SubjectListener;
import com.atguigu.ggkt.vod.mapper.SubjectMapper;
import com.atguigu.ggkt.vod.service.SubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 杰哥
 * @since 2022-07-30
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
    @Resource
    private SubjectListener subjectListener;
    //课程分类列表
    //懒加载，每次只查一层数据
    @Override
    public List<Subject> selectSubjectList(Long id) {
        //select * from subject where parent_id = 0
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        List<Subject> subjectList = baseMapper.selectList(wrapper);
        //遍历subject，得到每个subject对象，判断是否有下一层数据，有的话设置hasChildren=true
        for (Subject subject : subjectList) {
            //获取subject的id值
            Long subjectId = subject.getId();
            //查询是否有子节点
            boolean hasChild = this.hasChild(subjectId);
            subject.setHasChildren(hasChild);
        }
        return subjectList;
    }

    //课程分类导出
    @Override
    public void exportData(HttpServletResponse response) {
        try{
            //设置下载信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("课程分类", "utf-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");

            //查询所有的课程分类所有的数据
            List<Subject> subjectList = baseMapper.selectList(null);
            //List<Subject> --> List<SubjectEeVo>
            List<SubjectEeVo> subjectEeVoList = new LinkedList<>();
            for (Subject subject : subjectList) {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
//            subjectEeVo.setId(subject.getId());
//            subjectEeVo.setParentId(subject.getParentId());
                BeanUtils.copyProperties(subject, subjectEeVo); //只复制相同属性名称的属性
                subjectEeVoList.add(subjectEeVo);
            }
            //EasyExcel写操作
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
                    .sheet("课程分类")
                    .doWrite(subjectEeVoList);
        }catch (Exception e){
            e.printStackTrace();
            throw new CustomException(20001, "Excel表导出失败！");
        }
    }

    //课程分类导入
    @Override
    public boolean importData(MultipartFile file) {
        boolean flag = true;
        try {
            EasyExcel.read(file.getInputStream(), SubjectEeVo.class, subjectListener)
                    .sheet()
                    .doRead();
        }catch (Exception e){
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    //判断是否有下一层数据
    private boolean hasChild(Long subjectId) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", subjectId);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0;
    }
}
